import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { JVxeTypes, JVxeColumn } from '/@/components/jeecg/JVxeTable/types';
import { getDataSourceConfigList } from '@/api/codegen/dataSourceConfig';
import { ref,} from 'vue';
//列表数据
export const columns: BasicColumn[] = [
  {
    title: '数据源配置的编号',
    align: 'center',
    dataIndex: 'dataSourceConfigId',
  },

  {
    title: '表名称',
    align: 'center',
    dataIndex: 'tableName',
  },
  {
    title: '模块名',
    align: 'center',
    dataIndex: 'moduleCode',
  },
  {
    title: '业务名',
    align: 'center',
    dataIndex: 'businessCode',
  },
  {
    title: '类名称',
    align: 'center',
    dataIndex: 'className',
  },
  {
    title: '类描述',
    align: 'center',
    dataIndex: 'classComment',
  },
  {
    title: '作者',
    align: 'center',
    dataIndex: 'author',
  },
  {
    title: '模板类型',
    align: 'center',
    dataIndex: 'tableType',
  },
  {
    title: '父菜单编号',
    align: 'center',
    dataIndex: 'parentMenuId',
  },
  {
    title: '弹窗默认全屏',
    align: 'center',
    dataIndex: 'defaultFullscreen',
  },
  {
    title: '导入',
    align: 'center',
    dataIndex: 'importFlag',
  },
  {
    title: '导出',
    align: 'center',
    dataIndex: 'exportFlag',
  },
];
//查询数据
export const searchFormSchema: FormSchema[] = [
  {
    label: '生成场景',
    field: 'scene',
    component: 'Input',
  },
];

export const formSchema: FormSchema[] = [
  // TODO 主键隐藏字段，目前写死为ID
  { label: '', field: 'id', component: 'Input', show: false },
  {
    label: '表名',
    field: 'tableName',
    component: 'Input',
    colProps: { span: 12 },
    required: true,
    dynamicDisabled: ({ values }) => {
      return !!values.id;
    },
  },
  {
    label: '类描述',
    field: 'classComment',
    component: 'Input',
    colProps: { span: 12 },
    required: true,
  },
  {
    label: '模块名',
    field: 'moduleCode',
    component: 'Input',
    colProps: { span: 12 },
    required: true,
  },
  {
    label: '业务名',
    field: 'businessCode',
    component: 'Input',
    colProps: { span: 12 },
    required: true,
  },
  {
    label: '类名称',
    field: 'className',
    component: 'Input',
    colProps: { span: 12 },
    required: true,
  },
  {
    label: '模板类型',
    field: 'tableType',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'codegen_template_type',
      placeholder: '请选择模板类型',
      stringToNumber: false,
    },
    colProps: { span: 12 },
    required: true,
  },
  {
    label: '子表',
    field: 'subTables',
    component: 'Input',
    colProps: { span: 24 },
    slot: 'subTableSlot',
    ifShow: ({ values }) => values.tableType === '6',
  },
  {
    label: '左树',
    field: 'leftTreeHeadId',
    component: 'JDictSelectTag',
    colProps: { span: 12 },
    componentProps: {
      dictCode: 'onl_cgform_head,table_txt,id',
      params:{tableType:2},
      placeholder: '请选择左树',
      stringToNumber: true,
    },
    ifShow: ({ values }) => values.tableType === '3',
    dynamicRules: ({ values }) => {
      return values.tableType == '3' ? [{ required: true, message: '字段必填' }] : [];
    },
  },
  {
    label: '右表的外键名称',
    field: 'leftTreeKey',
    component: 'Input',
    colProps: { span: 12 },
    ifShow: ({ values }) => values.tableType === '3',
    dynamicRules: ({ values }) => {
      return values.tableType == '3' ? [{ required: true, message: '字段必填' }] : [];
    },
  },
  {
    label: '父菜单编号',
    field: 'parentMenuId',
    component: 'JTreeSelect',
    required:true,
    componentProps: {
      dict: 'sys_permission,name,id',
      pidField: 'parent_id',
      hasChildField: 'is_leaf',
      converIsLeafVal: 0,
    },
    colProps: { span: 12 },
  },
  {
    label: '弹窗默认全屏',
    field: 'defaultFullScreen',
    colProps: { span: 12 },
    component: 'JDictSelectTag',
    defaultValue: 'y',
    componentProps: {
      dictCode: 'is_open',
      type: 'radioButton',
    },
  },
  {
    label: '打印',
    field: 'printFlag',
    colProps: { span: 12 },
    component: 'JDictSelectTag',
    defaultValue: 'N',
    componentProps: {
      dictCode: 'is_open',
      type: 'radioButton',
    },
  },
  {
    label: '导入',
    field: 'importFlag',
    colProps: { span: 12 },
    component: 'JDictSelectTag',
    defaultValue: 'Y',
    componentProps: {
      dictCode: 'is_open',
      type: 'radioButton',
    },
  },
  {
    label: '导出',
    field: 'exportFlag',
    colProps: { span: 12 },
    component: 'JDictSelectTag',
    defaultValue: 'Y',
    componentProps: {
      dictCode: 'is_open',
      type: 'radioButton',
    },
  },
  {
    label: '备注',
    field: 'remark',
    component: 'InputTextArea',
    colProps: { span: 24 },
  },
];

//子表表格配置
export const codegenFormColumnColumns: JVxeColumn[] = [
  {
    title: '列名',
    key: 'columnName',
    type: JVxeTypes.normal,
    width: '120px',
  },
  {
    title: '字段名',
    key: 'javaField',
    type: JVxeTypes.input,
    width: '120px',
  },
  {
    title: '字段描述',
    key: 'dbFieldTxt',
    type: JVxeTypes.input,
    width: '120px',
    placeholder: '请输入${title}',
    fixed: 'left',
    defaultValue: '',
    validateRules: [{ required: true, message: '${title}不能为空' }],
  },
  {
    title: '列表显示',
    key: 'isShowList',
    type: JVxeTypes.checkbox,
    width: '75px',
    placeholder: '请输入${title}',
    customValue: [1, 0], // true ,false
    defaultChecked: false,
  },
  {
    title: '表单显示',
    key: 'isShowForm',
    type: JVxeTypes.checkbox,
    width: '75px',
    placeholder: '请输入${title}',
    customValue: [1, 0], // true ,false
    defaultChecked: false,
  },
  {
    title: '表单显示类型',
    key: 'htmlType',
    type: JVxeTypes.select,
    width: '120px',
    placeholder: '请输入${title}',
    options: [],
    dictCode: 'codegen_html_type',
    defaultValue: 'input',
    validateRules: [{ required: true, message: '${title}不能为空' }],
  },
  {
    title: '查询显示',
    key: 'isQuery',
    type: JVxeTypes.checkbox,
    width: '75px',
    placeholder: '请输入${title}',
    customValue: [1, 0], // true ,false
    defaultChecked: false,
  },
  {
    title: '查询显示类型',
    key: 'queryShowType',
    type: JVxeTypes.select,
    width: '120px',
    placeholder: '请输入${title}',
    options: [],
    dictCode: 'query_show_type',
    defaultValue: 'input',
  },
  {
    title: '是否必填',
    key: 'isRequired',
    type: JVxeTypes.checkbox,
    width: '75px',
    placeholder: '请输入${title}',
    customValue: [1, 0], // true ,false
    defaultChecked: false,
  },
  {
    title: '导入',
    key: 'importFlag',
    type: JVxeTypes.checkbox,
    width: '60px',
    placeholder: '请输入${title}',
    customValue: [1, 0], // true ,false
    defaultChecked: false,
  },
  {
    title: '导出',
    key: 'exportFlag',
    type: JVxeTypes.checkbox,
    width: '60px',
    placeholder: '请输入${title}',
    customValue: [1, 0], // true ,false
    defaultChecked: false,
  },
  {
    title: '字典Code',
    key: 'dictField',
    type: JVxeTypes.select,
    dictCode: 'gene_dict_code',
    width: '100px',
    placeholder: '请输入${title}',
    validateRules:[
      {
        handler({ cellValue, row, column }, callback, target) {
          let $htmlType = row.htmlType;
          let $htmlTypeArr = ['j_select','j_select_multi','j_search_select','j_tree_select','j_tree_select_multi','j_radio','j_checkbox','j_switch']
            if($htmlTypeArr.includes($htmlType)&&!cellValue){
              callback(false)  // false = 未通过校验
            }else{
              callback(true) // true = 通过验证
            }
        },
        message: '必填',
      }
    ],
  },
  {
    title: '表单扩展参数',
    key: 'componentExtJson',
    type: JVxeTypes.textarea,
    width: '120px',
    placeholder: '请输入${title}',
  },
  {
    title: '打印',
    key: 'printFlag',
    type: JVxeTypes.checkbox,
    width: '60px',
    placeholder: '请输入${title}',
    customValue: [1, 0], // true ,false
    defaultChecked: false,
  },
  {
    title: '打印组件类型',
    key: 'printCompType',
    type: JVxeTypes.select,
    width: '120px',
    placeholder: '请输入${title}',
    options: [],
    dictCode: 'print_comp_type',
    defaultValue: 'braid-txt',
  },
  {
    title: '单元格宽度',
    key: 'width',
    type: JVxeTypes.input,
    width: '100px',
    placeholder: '请输入${title}',
  },
 
  {
    title: '布局',
    type: JVxeTypes.select,
    key: 'layout',
    options: [],
    dictCode: 'codegen_layout',
    width: '100px',
    placeholder: '请输入${title}',
  },
  {
    title:'数据库字段',
    key: 'isDbField',
    type: JVxeTypes.checkbox,
    width: '120px',
    placeholder: '请输入${title}',
    defaultValue: 'y',
    customValue: ['y', 'n'],
  },
];

//子表表格配置
export const validateFormColumnColumns: JVxeColumn[] = [
  {
    title: '列名',
    key: 'columnName',
    type: JVxeTypes.normal,
    width: '120px',
    show: false,
  },
  {
    title: '字段描述',
    key: 'dbFieldTxt',
    type: JVxeTypes.normal,
    width: '120px',
    placeholder: '请输入${title}',
    defaultValue: '',
  },

  {
    title: '是否排序',
    key: 'sortFlag',
    type: JVxeTypes.checkbox,
    width: '75px',
    placeholder: '请输入${title}',
    customValue: [1, 0], // true ,false
    defaultChecked: false,
  },
  {
    title: '默认值',
    key: 'fieldDefaultValue',
    type: JVxeTypes.input,
    width: '100px',
    placeholder: '请输入${title}',
  },
  {
    title: '校验规则',
    key: 'validateRules',
    type: JVxeTypes.selectMultiple,
    width: '120px',
    options: [],
    dictCode: 'codegen_validate_rules',
  },
  {
    title: '提示语',
    key: 'placeholder',
    type: JVxeTypes.input,
    width: '100px',
  },
  {
    title: '动态显示函数',
    key: 'showFunc',
    type: JVxeTypes.input,
    width: '100px',
  },

  {
    title: '只读函数',
    key: 'disabledFunc',
    type: JVxeTypes.input,
    width: '100px',
  },
  {
    title: '改变事件函数',
    key: 'onChangeFunc',
    type: JVxeTypes.input,
    width: '100px',
  },
  {
    title: '单元格渲染函数',
    key: 'customRenderFunc',
    type: JVxeTypes.input,
    width: '100px',
  },
  {
    title: 'slot',
    key: 'slot',
    type: JVxeTypes.input,
    width: '100px',
  },
];

export const importTableColumns: BasicColumn[] = [
  {
    title: '表名称',
    dataIndex: 'name',
    width: 200,
  },
  {
    title: '表描述',
    dataIndex: 'comment',
    width: 120,
  },
];

export const importTableSearchFormSchema: FormSchema[] = [
  {
    label: '数据源',
    field: 'dataSourceConfigId',
    component: 'ApiSelect',
    required: true,
    componentProps: {
      api: getDataSourceConfigList,
      labelField: 'name',
      valueField: 'id',
    },
    colProps: { span: 8 },
  },
  {
    label: '表名称',
    field: 'name',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    label: '表描述',
    field: 'comment',
    component: 'Input',
    colProps: { span: 8 },
  },
];


export const jvxeSubTableSchema =  ref<JVxeColumn[]>([
  {
    title: 'id',
    key: 'id',
    width: 130,
    align: 'center',
    type: JVxeTypes.hidden,
  },
  {
    title: '表名',
    key: 'subHeadId',
    width: 130,
    align: 'center',
    type: JVxeTypes.select,
    dictCode:'onl_cgform_head,table_txt,id',
    params:{
      tableType:'5',
    }
  },
  {
    title: '关联字段',
    key: 'subTableKey',
    width: 130,
    align: 'center',
    type: JVxeTypes.input,
  },
  {
    title: 'java属性名',
    key: 'javaFieldName',
    width: 130,
    align: 'center',
    type: JVxeTypes.input,
  },
  {
    title: '显示名称',
    key: 'javaFieldLabel',
    width: 130,
    align: 'center',
    type: JVxeTypes.input,
  },
  {
    title: '顺序号',
    key: 'subTableOrderNum',
    width: 130,
    align: 'center',
    type: JVxeTypes.inputNumber,
  },
  {
    title: '操作',
    key: 'action',
    type: JVxeTypes.slot,
    fixed: 'right',
    minWidth: 120,
    align: 'center',
    slotName: 'tableAction',
  },
]);
